﻿Public Class frmInvoiceDetail

#Region "Method"

#Region "Overrides"
    ''' <summary>
    ''' Sub đưa dữ liệu hiển thị trên control
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 11/06/2012</remarks>
    Protected Overrides Sub BindingControl()
        With BusinessObject.dsFMS.Invoice
            LoadCustomer()
            LoadEmployee()
            LoadCategory()
            dteInvoiceDate.DataBindings.Add("Text", bsFMS, .InvoiceCreatedDateColumn.ColumnName)
            txtInvoiceAlias.DataBindings.Add("Text", bsFMS, .InvoiceAliasColumn.ColumnName)
            txtDescription.DataBindings.Add("Text", bsFMS, .InvoiceDescriptionColumn.ColumnName)
            If ActionMode <> clscommon.ActionMode.Add Then
                rdbTaxRate.Value = bsFMS.Current(.InvoiceTaxColumn.ColumnName)
                cboCustomer.Value = bsFMS.Current(.CustomerIDColumn.ColumnName)
                cboEmployee.Value = bsFMS.Current(.EmployeeIDColumn.ColumnName)
            End If
            LoadInvoiceDetail()
        End With
    End Sub

    ''' <summary>
    ''' Sub xóa binding dữ liệu trên control
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 11/06/2012</remarks>
    Protected Overrides Sub ClearDataBingding()
        With Me
            .dteInvoiceDate.DataBindings.Clear()
            .txtInvoiceAlias.DataBindings.Clear()
            .txtDescription.DataBindings.Clear()
        End With
    End Sub

    ''' <summary>
    ''' Sub set thuộc tính cho các control là read only hay không
    ''' </summary>
    ''' <param name="isReadOnly">Ở chế độ read only thì đặt là true và ngược lại</param>
    ''' <remarks>Nguyễn Huy Thường added on 11/06/2012</remarks>
    Protected Overrides Sub SetControlEditable(ByVal isReadOnly As Boolean)
        MyBase.SetControlEditable(isReadOnly)
        With Me
            .cboCategory.ButtonsRight(0).Visible = Not isReadOnly
            .cboCategory.ReadOnly = isReadOnly
            .drpItem.ButtonsRight(0).Visible = Not isReadOnly
            .drpItem.ReadOnly = isReadOnly
            .txtQuantity.ButtonsLeft(0).Visible = Not isReadOnly
            .txtQuantity.ButtonsRight(0).Visible = Not isReadOnly
            .txtQuantity.ReadOnly = isReadOnly
            .txtSellPrice.ReadOnly = isReadOnly
            .btnAddInvoiceDetail.Visible = Not isReadOnly
            .txtServiceAlias.ReadOnly = isReadOnly
            .txtServiceDescription.ReadOnly = isReadOnly
            .txtServiceQuantity.ReadOnly = isReadOnly
            .txtServiceCost.ReadOnly = isReadOnly
            .btnAddService.Visible = Not isReadOnly
            .dteInvoiceDate.ReadOnly = isReadOnly
            .txtInvoiceAlias.ReadOnly = isReadOnly
            .cboCustomer.ButtonsRight(0).Visible = Not isReadOnly
            .cboCustomer.ReadOnly = isReadOnly
            .cboEmployee.ButtonsRight(0).Visible = Not isReadOnly
            .cboEmployee.ReadOnly = isReadOnly
            .rdbTaxRate.Enabled = Not isReadOnly
            .btnPaidHistory.Visible = Not isReadOnly
            .txtDescription.ReadOnly = isReadOnly
            .btnPaidEnough.Visible = isReadOnly
            .btnPaidHistory.Visible = isReadOnly
            If isReadOnly Then
                .lblDescription.Location.Offset(6, 288)
                .txtDescription.Location.Offset(110, 285)
                .grdInvoiceDetail.DisplayLayout.Override.CellClickAction = Infragistics.Win.UltraWinGrid.CellClickAction.CellSelect
            Else
                .lblDescription.Location.Offset(6, 350)
                .txtDescription.Location.Offset(110, 347)
                .grdInvoiceDetail.DisplayLayout.Override.CellClickAction = Infragistics.Win.UltraWinGrid.CellClickAction.EditAndSelectText
                If ActionMode = clscommon.ActionMode.Add Then
                    BusinessObject.dsFMS.InvoiceDetail.Clear()
                    .txtPaid.ReadOnly = True
                    .txtSellPrice.Text = String.Empty
                    .txtQuantity.Text = 1
                    .cboCategory.SelectedIndex = 0
                    .cboCustomer.SelectedIndex = 0
                    .cboEmployee.SelectedIndex = 0
                    .rdbTaxRate.CheckedIndex = 0
                Else
                    .txtPaid.ReadOnly = False
                End If
            End If
        End With
    End Sub

    ''' <summary>
    ''' Sub xác định vị trí của bản ghi hiện thời và load lại dữ liệu
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 11/06/2012</remarks>
    Protected Overrides Sub RefreshData()
        MyBase.RefreshData()
        With BusinessObject.dsFMS.Invoice
            If Not IsNothing(bsFMS.Current) And ActionMode <> clscommon.ActionMode.Add Then
                LoadInvoiceDetail()
                rdbTaxRate.Value = bsFMS.Current(.InvoiceTaxColumn.ColumnName)
                cboCustomer.Value = bsFMS.Current(.CustomerIDColumn.ColumnName)
                cboEmployee.Value = bsFMS.Current(.EmployeeIDColumn.ColumnName)
            End If
        End With
    End Sub

    ''' <summary>
    ''' Sub đặt giá trị cho khóa ngoại
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 18/06/2012</remarks>
    Protected Overrides Sub SetDataValue()
        With BusinessObject
            .CurrentRow(.dsFMS.Invoice.CustomerIDColumn.ColumnName) = cboCustomer.Value
            .CurrentRow(.dsFMS.Invoice.EmployeeIDColumn.ColumnName) = cboEmployee.Value
            .CurrentRow(.dsFMS.Invoice.InvoiceTaxColumn.ColumnName) = rdbTaxRate.Value
        End With
    End Sub

    ''' <summary>
    ''' Hàm lưu dữ liệu vào database (add hoặc edit)
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 11/06/2012</remarks>
    Protected Overrides Sub SaveData()
        If grdInvoiceDetail.Rows.Count > 0 Then
            bsInvoiceDetail.EndEdit()
            With BusinessObject
                MyBase.SaveData()
                If Not .CheckAvailable(.dsFMS.Invoice.InvoiceIDColumn.ColumnName, .CurrentRow) Then
                    SavePayment()
                End If
            End With
        Else
            clscommon.ShowInformation(My.Resources.NoItemInInvoice)
        End If
    End Sub

    ''' <summary>
    ''' Sub xem phiếu nhập kho hiện tại
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 18/06/2012</remarks>
    Protected Overrides Sub ViewDetail()
        With BusinessObject
            '.dsXHGM.DisplayInvoice.Clear()
            '.FillReport(.dsXHGM.DisplayInvoice.TableName, bsXHGM.Current(.dsXHGM.Invoice.InvoiceIDColumn.ColumnName), Nothing, Nothing)
            'Dim viewInvoice As New frmInvoiceDetailView
            'With viewInvoice
            '    .DataSource = BusinessObject.dsXHGM.DisplayInvoice
            '    .Parameter = clsCommon.MoneyReader(Long.Parse(txtTotalAmount.Text))
            '    .Title = String.Format(My.Resources.InvoiceDetailTitle, dteDate.DateTime.Day.ToString & "/" & dteDate.DateTime.Month.ToString & "/" & dteDate.DateTime.Year.ToString, cboPlateNumber.Text)
            '    .Owner = Me
            '    .ShowDialog()
            'End With
        End With
    End Sub

    ''' <summary>
    ''' Sub in phiếu nhập kho hiện tại
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 18/06/2012</remarks>
    Protected Overrides Sub PrintDetail()
        With BusinessObject
            '.dsXHGM.DisplayInvoice.Clear()
            '.FillReport(.dsXHGM.DisplayInvoice.TableName, bsXHGM.Current(.dsXHGM.Invoice.InvoiceIDColumn.ColumnName), Nothing, Nothing)
            'Dim printInvoice As New frmInvoiceDetailPrint
            'With printInvoice
            '    .ReportType = clsCommon.ReportType.SmallInvoice
            '    .DataSource = BusinessObject.dsXHGM.DisplayInvoice
            '    .Parameter = clsCommon.MoneyReader(Long.Parse(txtTotalAmount.Text))
            '    .Owner = Me
            '    .ShowDialog()
            'End With
        End With
    End Sub
#End Region

    ''' <summary>
    ''' Sub đưa danh sách các mặt hàng trong lô hàng lên grdShipmentDetail
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 11/06/2012</remarks>
    Private Sub LoadInvoiceDetail()
        With BusinessObject
            .dsFMS.InvoiceDetail.Clear()
            .GetDataByValue(.dsFMS.InvoiceDetail.TableName, .dsFMS.Invoice.InvoiceIDColumn.ColumnName, bsFMS.Current(.dsFMS.Invoice.InvoiceIDColumn.ColumnName))
            bsInvoiceDetail.DataSource = .dsFMS
            bsInvoiceDetail.DataMember = .dsFMS.InvoiceDetail.TableName
            grdInvoiceDetail.DataSource = bsInvoiceDetail
        End With
    End Sub

    ''' <summary>
    ''' Sub đưa danh sách nhân viên hiển thị lên cboReceiver và cboImporter
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 06/03/2012</remarks>
    Private Sub LoadEmployee()
        With BusinessObject
            .dsFMS.Employee.Clear()
            .GetTableData(.dsFMS.Employee.TableName)
            cboEmployee.DataSource = .dsFMS.Employee
            cboEmployee.DisplayMember = .dsFMS.Employee.EmployeeNameColumn.ColumnName
            cboEmployee.ValueMember = .dsFMS.Employee.EmployeeIDColumn.ColumnName
            If cboEmployee.Items.Count > 0 Then
                cboEmployee.SelectedIndex = 0
            Else
                cboEmployee.Text = String.Empty
            End If
        End With
    End Sub

    ''' <summary>
    ''' Sub lấy dữ liệu từ bảng Customer đưa vào dataset và hiển thị trên cboCustomer
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 11/06/2010</remarks>
    Private Sub LoadCustomer()
        With BusinessObject
            .dsFMS.Customer.Clear()
            .GetTableData(.dsFMS.Customer.TableName)
            cboCustomer.DataSource = .dsFMS.Customer
            cboCustomer.DisplayMember = .dsFMS.Customer.CustomerNameColumn.ColumnName
            cboCustomer.ValueMember = .dsFMS.Customer.CustomerIDColumn.ColumnName
            If cboCustomer.Items.Count > 0 Then
                cboCustomer.SelectedIndex = 0
            Else
                cboCustomer.Text = String.Empty
            End If
        End With
    End Sub

    ''' <summary>
    ''' Sub lấy dữ liệu từ bảng Category đưa vào dataset và hiển thị trên cboCategory
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 11/06/2010</remarks>
    Private Sub LoadCategory()
        With BusinessObject
            .dsFMS.Category.Clear()
            .GetTableData(BusinessObject.dsFMS.Category.TableName)
            cboCategory.DataSource = .dsFMS.Category
            cboCategory.DisplayMember = .dsFMS.Category.CategoryNameColumn.ColumnName
            cboCategory.ValueMember = .dsFMS.Category.CategoryIDColumn.ColumnName
            If cboCategory.Items.Count > 0 Then
                cboCategory.SelectedIndex = 0
                LoadItem()
            Else
                cboCategory.Text = String.Empty
            End If
        End With
    End Sub

    ''' <summary>
    ''' Sub đưa danh sách các mặt hàng lên drpItem
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 11/06/2012</remarks>
    Private Sub LoadItem()
        With BusinessObject
            .dsFMS.Item.Clear()
            .GetDataByValue(.dsFMS.Item.TableName, .dsFMS.Item.CategoryIDColumn.ColumnName, cboCategory.Value)
            drpItem.DataSource = .dsFMS.Item
            drpItem.DisplayMember = String.Format(My.Resources.TableInfo, .dsFMS.Item.TableName)
            drpItem.ValueMember = .dsFMS.Item.ItemIDColumn.ColumnName
            If drpItem.Rows.Count > 0 Then
                drpItem.SelectedRow = drpItem.Rows(0)
            Else
                drpItem.Text = String.Empty
            End If
        End With
    End Sub

    ''' <summary>
    ''' Sub đưa danh sách các mặt hàng lên drpItem
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 11/06/2012</remarks>
    Private Sub LoadShipment()
        With BusinessObject
            .dsFMS.ShipmentDetail.Clear()
            .GetDataByValue(.dsFMS.ShipmentDetail.TableName, .dsFMS.Item.ItemIDColumn.ColumnName, drpItem.Value)
            drpShipment.DataSource = .dsFMS.ShipmentDetail
            drpShipment.DisplayMember = .dsFMS.Shipment.ShipmentAliasColumn.ColumnName
            drpShipment.ValueMember = .dsFMS.ShipmentDetail.ShipmentDetailIDColumn.ColumnName
            If drpShipment.Rows.Count > 0 Then
                drpShipment.SelectedRow = drpShipment.Rows(0)
            Else
                drpShipment.Text = String.Empty
            End If
        End With
    End Sub

    ''' <summary>
    ''' Hàm tạo mới một dòng trong bảng ShipmentDetail trên dataset
    ''' </summary>
    ''' <returns>Trả lại true nếu thành công và ngược lại</returns>
    ''' <remarks>Nguyễn Huy Thường added on 18/06/2012</remarks>
    Private Function AddItemDetail() As Boolean
        Dim result As Boolean = False
        Try
            With BusinessObject.dsFMS
                If tabItemService.SelectedTab.Index = 0 Then
                    If IsNothing(drpItem.SelectedRow) Then
                        drpItem.Focus()
                        clscommon.ShowInformation(String.Format(My.Resources.ItemNotSelected, "mặt hàng"))
                        Return False
                    ElseIf Not clscommon.CheckQuantity(txtQuantity.Text) Then
                        txtQuantity.Focus()
                        txtQuantity.SelectAll()
                        Return False
                    ElseIf Not clscommon.CheckMoney(txtSellPrice.Text) Then
                        txtSellPrice.Focus()
                        txtSellPrice.SelectAll()
                        Return False
                    Else
                        Dim itemAdded As Boolean
                        For Each row As Infragistics.Win.UltraWinGrid.UltraGridRow In grdInvoiceDetail.Rows
                            If Not String.IsNullOrEmpty(row.Cells(.InvoiceDetail.InvoiceDetailIDColumn.ColumnName).Value.ToString()) Then
                                If row.Cells(.ShipmentDetail.ShipmentDetailIDColumn.ColumnName).Value = drpShipment.SelectedRow.Cells("ShipmentDetailID").Value Then
                                    row.Activate()
                                    itemAdded = True
                                End If
                            End If
                        Next
                        If itemAdded Then
                            grdInvoiceDetail.ActiveRow.Cells(.InvoiceDetail.InvoiceDetailQuantityColumn.ColumnName).Value += Decimal.Parse(txtQuantity.Text)
                            grdInvoiceDetail.ActiveRow.Cells(.InvoiceDetail.InvoiceDetailSellPriceColumn.ColumnName).Value = Decimal.Parse(txtSellPrice.Text)
                            bsInvoiceDetail.Position = grdInvoiceDetail.ActiveRow.Index
                            result = True
                        Else
                            Dim drNewRow As BL.FMSDataSet.InvoiceDetailRow = .InvoiceDetail.NewInvoiceDetailRow
                            With drNewRow
                                .InvoiceDetailID = Guid.NewGuid
                                .InvoiceID = BusinessObject.CurrentRow(BusinessObject.dsFMS.Invoice.InvoiceIDColumn.ColumnName)
                                .ShipmentDetailID = drpShipment.Value
                                .InvoiceDetailAlias = drpItem.SelectedRow.Cells("ItemAlias").Value
                                .InvoiceDetailName = drpItem.SelectedRow.Cells("ItemName").Value
                                .InvoiceDetailQuantity = Decimal.Parse(txtQuantity.Text)
                                .InvoiceDetailBuyPrice = Decimal.Parse(drpItem.SelectedRow.Cells("BuyPrice").Value)
                                .InvoiceDetailSellPrice = Decimal.Parse(txtSellPrice.Text)
                                .InvoiceDetailCreatedDate = Today
                                .SetInvoiceDetailDeletedDateNull()
                                .InvoiceDetailStatus = True
                                .SetInvoiceDetailDescriptionNull()
                            End With
                            .InvoiceDetail.Rows.Add(drNewRow)
                            bsInvoiceDetail.Position = grdInvoiceDetail.Rows.Count - 1
                            result = True
                        End If
                    End If
                ElseIf tabItemService.SelectedTab.Index = 1 Then
                    If txtServiceAlias.Text = String.Empty Then
                        Return False
                    ElseIf txtServiceDescription.Text = String.Empty Then
                        Return False
                    ElseIf Not clscommon.CheckQuantity(txtServiceQuantity.Text) Then
                        Return False
                    ElseIf Not clscommon.CheckMoney(txtServiceCost.Text) Then
                        Return False
                    Else
                        Dim itemAdded As Boolean
                        For Each row As Infragistics.Win.UltraWinGrid.UltraGridRow In grdInvoiceDetail.Rows
                            If Not String.IsNullOrEmpty(row.Cells(.InvoiceDetail.InvoiceDetailIDColumn.ColumnName).Value.ToString()) Then
                                If row.Cells(.InvoiceDetail.InvoiceDetailAliasColumn.ColumnName).Value = "PNDV" + txtServiceAlias.Text Then
                                    row.Activate()
                                    itemAdded = True
                                End If
                            End If
                        Next
                        If itemAdded Then
                            grdInvoiceDetail.ActiveRow.Cells(.InvoiceDetail.InvoiceDetailQuantityColumn.ColumnName).Value += Decimal.Parse(txtServiceQuantity.Text)
                            grdInvoiceDetail.ActiveRow.Cells(.InvoiceDetail.InvoiceDetailSellPriceColumn.ColumnName).Value = Decimal.Parse(txtServiceCost.Text)
                            bsInvoiceDetail.Position = grdInvoiceDetail.ActiveRow.Index
                            result = True
                        Else
                            Dim drNewRow As BL.FMSDataSet.InvoiceDetailRow = .InvoiceDetail.NewInvoiceDetailRow
                            With drNewRow
                                .InvoiceDetailID = Guid.NewGuid
                                .InvoiceID = BusinessObject.CurrentRow(BusinessObject.dsFMS.Invoice.InvoiceIDColumn.ColumnName)
                                .SetShipmentDetailIDNull()
                                .InvoiceDetailAlias = "PNDV" + txtServiceAlias.Text
                                .InvoiceDetailName = txtServiceDescription.Text
                                .InvoiceDetailQuantity = Decimal.Parse(txtServiceQuantity.Text)
                                .InvoiceDetailBuyPrice = Decimal.Parse(txtServiceAlias.Text + "000")
                                .InvoiceDetailSellPrice = Decimal.Parse(txtServiceCost.Text)
                                .InvoiceDetailCreatedDate = Today
                                .SetInvoiceDetailDeletedDateNull()
                                .InvoiceDetailStatus = True
                                .SetInvoiceDetailDescriptionNull()
                            End With
                            .InvoiceDetail.Rows.Add(drNewRow)
                            bsInvoiceDetail.Position = grdInvoiceDetail.Rows.Count - 1
                            result = True
                        End If
                    End If
                End If
            End With
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
            Return False
        End Try
        Return result
    End Function

    ''' <summary>
    ''' Sub lưu lại thanh toán
    ''' </summary>
    ''' <remarks>Nguyễn Huy Thường added on 05/07/2012</remarks>
    Private Sub SavePayment()
        With BusinessObject
            Dim drNewRow As BL.FMSDataSet.PaymentRow = .dsFMS.Payment.NewPaymentRow
            With drNewRow
                .PaymentID = Guid.NewGuid
                .InvoiceID = BusinessObject.CurrentRow(BusinessObject.dsFMS.Invoice.InvoiceIDColumn.ColumnName)
                .PaymentPaid = Decimal.Parse(txtPaid.Text)
                .PaymentDate = Today
                .SetPaymentDeletedDateNull()
                .PaymentStatus = True
                .SetPaymentDescriptionNull()
            End With
            .dsFMS.Payment.Rows.Add(drNewRow)
            If .EditRecord(drNewRow) Then
                .dsFMS.Payment.AcceptChanges()
            Else
                clscommon.ShowException(String.Format(My.Resources.SaveError, "số tiền đã thanh toán"))
                .dsFMS.Payment.RejectChanges()
            End If
        End With
    End Sub
#End Region

#Region "Event"

    ''' <summary>
    ''' Load danh sách mặt hàng khi chủng loại thay đổi
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 11/06/2012</remarks>
    Private Sub cboCategory_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCategory.ValueChanged
        Try
            If cboCategory.IsItemInList Then
                LoadItem()
            End If
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Hiện form dlgNewCategory khi người dùng bấm vào nút tạo mới trong cboCategory trên form frmShipmentDetail
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 11/06/2012</remarks>
    Private Sub cboCategory_EditorButtonClick(ByVal sender As System.Object, ByVal e As Infragistics.Win.UltraWinEditors.EditorButtonEventArgs) Handles cboCategory.EditorButtonClick
        Try
            If e.Button.Key = "NewCategory" Then
                Using addCategory As New dlgNewCategory() With {.Owner = Me, .BusinessObject = Me.BusinessObject}
                    Dim currentCategory As String = cboCategory.Text
                    addCategory.ShowDialog()
                    If Not String.IsNullOrEmpty(addCategory.CategoryName) Then
                        BusinessObject.dsFMS.Category.Clear()
                        LoadCategory()
                        If ActionMode <> clscommon.ActionMode.View Then
                            cboCategory.SelectedText = addCategory.CategoryName
                        Else
                            cboCategory.SelectedText = currentCategory
                        End If
                    End If
                End Using
            End If
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Lấy ra số lượng tồn kho của mặt hàng đang được chọn trong drpItem và load danh sách lô hàng
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 12/06/2012</remarks>
    Private Sub drpItem_RowSelected(ByVal sender As System.Object, ByVal e As Infragistics.Win.UltraWinGrid.RowSelectedEventArgs) Handles drpItem.RowSelected
        Try
            If Not IsNothing(drpItem.SelectedRow) Then
                LoadShipment()
                txtStore.Text = drpItem.SelectedRow.Cells("Quantity").Value
                If ActionMode <> clscommon.ActionMode.View Then
                    txtSellPrice.Text = drpItem.SelectedRow.Cells("SellPrice").Value
                    txtQuantity.Text = 1
                End If
            End If
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Thuế suất thay đổi thì tính toán lại tổng tiền sau thuế
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 12/06/2012</remarks>
    Private Sub rdbTaxRate_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbTaxRate.ValueChanged
        Try
            With grdInvoiceDetail
                If IsNumeric(.Rows.SummaryValues(0).Value) Then
                    txtTaxAmount.Text = (.Rows.SummaryValues(0).Value * rdbTaxRate.Value / 100).ToString
                    txtGrandTotal.Text = (.Rows.SummaryValues(0).Value + (.Rows.SummaryValues(0).Value * rdbTaxRate.Value / 100)).ToString
                    txtPaid.Text = txtGrandTotal.Text
                    txtDebt.Text = (Decimal.Parse(txtGrandTotal.Text) - Decimal.Parse(txtPaid.Text)).ToString
                End If
            End With
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Tăng hoặc giảm số lượng bằng right hay left button
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 12/06/2012</remarks>
    Private Sub txtQuantity_EditorButtonClick(ByVal sender As System.Object, ByVal e As Infragistics.Win.UltraWinEditors.EditorButtonEventArgs) Handles txtQuantity.EditorButtonClick
        Try
            With txtQuantity
                If e.Button.Key = "Add" Then
                    .Text = (Int16.Parse(.Text) + 1).ToString
                ElseIf e.Button.Key = "Subtract" Then
                    If Integer.Parse(.Text) <= 1 Then
                        .Text = 1
                    Else
                        .Text = (Int16.Parse(.Text) - 1).ToString
                    End If
                End If
            End With
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Nếu ô số lượng bị xóa thì trả lại giá trị là 1
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 12/06/2012</remarks>
    Private Sub txtQuantity_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtQuantity.TextChanged
        Try
            If String.IsNullOrEmpty(txtQuantity.Text) Then
                txtQuantity.Text = 1
            End If
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Tổng tiền thay đổi thì tính toán lại tổng tiền sau thuế
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 12/06/2012</remarks>
    Private Sub grdInvoiceDetail_SummaryValueChanged(ByVal sender As System.Object, ByVal e As Infragistics.Win.UltraWinGrid.SummaryValueChangedEventArgs) Handles grdInvoiceDetail.SummaryValueChanged
        Try
            With grdInvoiceDetail
                If IsNumeric(.Rows.SummaryValues(0).Value) Then
                    txtTotalAmount.Text = .Rows.SummaryValues(0).Value
                    txtTaxAmount.Text = (.Rows.SummaryValues(0).Value * rdbTaxRate.Value / 100).ToString
                    txtGrandTotal.Text = (.Rows.SummaryValues(0).Value + (.Rows.SummaryValues(0).Value * rdbTaxRate.Value / 100)).ToString
                    txtPaid.Text = txtGrandTotal.Text
                    txtDebt.Text = (Decimal.Parse(txtGrandTotal.Text) - Decimal.Parse(txtPaid.Text)).ToString
                End If
            End With
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ' ''' <summary>
    ' ''' Khi một dòng trong grdInvoiceDetail được chọn (Active) thì update giá trị trên các control khác (Chỉ áp dụng ở chế độ view)
    ' ''' </summary>
    ' ''' <param name="sender"></param>
    ' ''' <param name="e"></param>
    ' ''' <remarks>Nguyễn Huy Thường added on 12/06/2012</remarks>
    'Private Sub grdInvoiceDetail_AfterRowActivate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles grdInvoiceDetail.AfterRowActivate
    '    Try
    '        With BusinessObject.dsFMS
    '            If ActionMode = clscommon.ActionMode.View Then
    '                cboCategory.Value = grdInvoiceDetail.ActiveRow.Cells(.Item.CategoryIDColumn.ColumnName).Value
    '                drpItem.Value = grdInvoiceDetail.ActiveRow.Cells(.Item.ItemIDColumn.ColumnName).Value
    '                txtQuantity.Text = grdInvoiceDetail.ActiveRow.Cells(.ShipmentDetail.ShipmentDetailQuantityColumn.ColumnName).Value
    '                txtSellPrice.Text = grdInvoiceDetail.ActiveRow.Cells(.ShipmentDetail.ShipmentDetailSellPriceColumn.ColumnName).Value
    '                If Not String.IsNullOrEmpty(grdInvoiceDetail.ActiveRow.Cells(.ShipmentDetail.ShipmentDetailSellPriceColumn.ColumnName).Value.ToString) Then
    '                Else

    '                End If
    '            End If
    '        End With
    '    Catch ex As Exception
    '        clscommon.ShowException(ex.Message)
    '    End Try
    'End Sub

    ''' <summary>
    ''' Thêm một mặt hàng vào phiếu nhập kho
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 12/06/2012</remarks>
    Private Sub btnAddShipmentDetail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddInvoiceDetail.Click, btnPaidHistory.Click, btnPaidEnough.Click
        Try
            If AddItemDetail() Then
                With BusinessObject.dsFMS.InvoiceDetail
                    If IsDBNull(bsInvoiceDetail.Current(.InvoiceDetailAliasColumn.ColumnName)) Then
                        bsInvoiceDetail.Current(.InvoiceDetailAliasColumn.ColumnName) = drpItem.SelectedRow.Cells(BusinessObject.dsFMS.Item.ItemAliasColumn.ColumnName).Value
                        bsInvoiceDetail.Current(.InvoiceDetailNameColumn.ColumnName) = drpItem.SelectedRow.Cells(BusinessObject.dsFMS.Item.ItemNameColumn.ColumnName).Value
                    End If
                End With
                cboCategory.Focus()
                txtQuantity.Text = 1
            End If
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub

    ''' <summary>
    ''' Thêm một dịch vụ vào phiếu nhập kho
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>Nguyễn Huy Thường added on 04/07/2012</remarks>
    Private Sub btnAddService_Click(sender As System.Object, e As System.EventArgs) Handles btnAddService.Click
        Try
            With BusinessObject.dsFMS.InvoiceDetail
                If AddItemDetail() Then
                    If IsDBNull(bsInvoiceDetail.Current(.InvoiceDetailAliasColumn.ColumnName)) Then
                        bsInvoiceDetail.Current(.InvoiceDetailAliasColumn.ColumnName) = drpItem.SelectedRow.Cells(BusinessObject.dsFMS.Item.ItemAliasColumn.ColumnName).Value
                        bsInvoiceDetail.Current(.InvoiceDetailNameColumn.ColumnName) = drpItem.SelectedRow.Cells(BusinessObject.dsFMS.Item.ItemNameColumn.ColumnName).Value
                    End If
                    txtServiceAlias.Focus()
                    txtServiceAlias.Text = String.Empty
                    txtServiceDescription.Text = String.Empty
                    txtServiceQuantity.Text = 1
                    txtServiceCost.Text = String.Empty
                End If
            End With
        Catch ex As Exception
            clscommon.ShowException(ex.Message)
        End Try
    End Sub
#End Region
End Class
